package com.fastloan.mgt.jx.app;

import com.alibaba.fastjson.JSON;
import com.fastloan.common.model.CheckResult;
import com.fastloan.mgt.application.approve.service.CreditApproveService;
import com.fastloan.mgt.application.base.model.ApplicationVo;
import com.fastloan.mgt.customer.model.CustomerAmount;
import com.fastloan.mgt.customer.service.CustomerAmountService;
import com.fastloan.mgt.customer.service.ZyCustomerService;
import com.fastloan.mgt.customer.model.CustomerCredit;
import com.fastloan.mgt.jx.model.dto.JxCreditDTO;
import com.fastloan.mgt.jx.service.JxService;
import com.fastloan.mgt.product.basics.model.Product;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;

/**
 * 前端调用接口
 *
 * @author chenkai
 * createTime 2018-01-2018/1/5 10:36
 */
@RestController
@RequestMapping("/api/app/jxCredit/anon")
public class JxCreditAppController extends BaseController<JxCreditDTO> {

    @Autowired
    private ZyCustomerService customerService;

    @Autowired
    private JxService jxService;

    @Autowired
    private CustomerAmountService customerAmountService;

    @Autowired
    private CreditApproveService creditApproveService;


    /**
     * 提交进件(预授信)
     *
     * @param applicationVo 进件
     * @return 结果
     */
    @RequestMapping("/submit")
    public ResultTo submitApplication(ApplicationVo applicationVo, CustomerCredit customerCredit, String agentOrderId) {
        CheckResult checkResult = jxService.submitCreditApplication(applicationVo, customerCredit, agentOrderId);
        if (!checkResult.isCheckResult()) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, checkResult.getCheckMsg());
        }
        return new ResultTo().setData(checkResult.getData());
    }

    /**
     * 预授信审批
     *
     * @return
     */
    @RequestMapping("/approve")
    public ResultTo getApprove(CustomerAmount creditAmount, Integer status) {
        try {
            creditApproveService.approve(creditAmount, status);
            return new ResultTo();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("请求失败,  错误原因:{}", e.getMessage());
            return new ResultTo(ResultEnum.OPERATION_FAILED, e.getMessage());
        }
    }

    /**
     * 获取预授信审批
     *
     * @return
     */
    @RequestMapping("/getApprove")
    public ResultTo getApprove() {
        try {
            return dataPage(customerAmountService, CustomerAmount.class, "jxApprove");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("请求失败,  错误原因:{}", e.getMessage());
            return new ResultTo(ResultEnum.OPERATION_FAILED, e.getMessage());
        }
    }

    /**
     * 获取审批信息
     *
     * @param applicationId
     * @return
     */
    @RequestMapping("/getApprove/{applicationId}")
    public ResultTo getApproveInfo(@PathVariable String applicationId) {
        try {
            return new ResultTo().setData(customerAmountService.queryApproveInfo(applicationId));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("请求失败,  错误原因:{}", e.getMessage());
            return new ResultTo(ResultEnum.OPERATION_FAILED, e.getMessage());
        }
    }

    /**
     * 提交进件
     *
     * @param applicationVo
     * @return
     */
    @RequestMapping("/submit/jx")
    public ResultTo submitJxApplication(ApplicationVo applicationVo, String agentOrderId) {
        CheckResult checkResult = jxService.submitJxApplication(applicationVo, agentOrderId);
        if (!checkResult.isCheckResult()) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, checkResult.getCheckMsg());
        }
        return new ResultTo().setData(checkResult.getData());
    }


    /**
     * 新建产品
     * applicationVo
     *
     * @param product
     * @param file
     * @return
     */
    @RequestMapping("/createProduct")
    public ResultTo createProduct(Product product, MultipartFile[] file) {
        CheckResult checkResult = jxService.insertProduct(product, file);
        if (!checkResult.isCheckResult()) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, checkResult.getCheckMsg());
        }
        return new ResultTo();
    }

    /**
     * 获取客户信息
     *
     * @param customerId
     * @return
     */
    @GetMapping("/{customerId}")
    public ResultTo getCustomerInfo(@PathVariable String customerId) {
        return new ResultTo().setData(customerService.selectByPrimaryKey(customerId));
    }

    /**
     * 获取IPC自动填充内容
     *
     * @param productId
     * @param customerId
     * @return
     */
    @GetMapping("/{productId}/{customerId}")
    public ResultTo getCustomerIPCInFo(@PathVariable String productId, @PathVariable String customerId) {
        CheckResult checkResult = jxService.customerIPC(productId, customerId);
        if (!checkResult.isCheckResult()) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, checkResult.getCheckMsg());
        }
        return new ResultTo().setData(checkResult.getData());
    }

    /**
     * 线下预授信
     *
     * @param jxCreditDTO
     * @param creditAmount
     * @return
     */
    @PostMapping("/offline")
    public ResultTo offlineCreditApplication(String jxCreditDTO, BigDecimal creditAmount, CustomerCredit customerCredit) {
        CheckResult checkResult = jxService.offlineCreditApplication(JSON.parseObject(jxCreditDTO, JxCreditDTO.class), creditAmount, customerCredit);
        if (!checkResult.isCheckResult()) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "不能重复申请");
        }
        return new ResultTo();
    }

}
